package com.yanqu.road.dao.impl.activity.patronsfund;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.patronsfund.PatronsFundConditionCom;
import com.yanqu.road.entity.activity.patronsfund.PatronsFundUser;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PatronsFundConditionComDaoImpl extends TempDataObjectDao<PatronsFundConditionCom> {

    public static String selectSql = "select * from t_u_patrons_fund_condition_com where userId = ? and activityId = ?";
    public Map<Integer, PatronsFundConditionCom> getPatronsFundConditionCom(long userId, int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, PatronsFundConditionCom> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PatronsFundConditionCom data = parse(rs);
                map.put(data.getConditionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertSql = "INSERT INTO `t_u_patrons_fund_condition_com`(`userId`, `patronsId`, `activityId`, `conditionType`, `conditionId`, `createTime`) VALUES (?, ?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(PatronsFundConditionCom data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getConditionType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getConditionId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(PatronsFundConditionCom data) {
        return false;
    }

    @Override
    protected PatronsFundConditionCom parseData(ResultSet rs) throws SQLException {
        PatronsFundConditionCom data = new PatronsFundConditionCom();
        data.setUserId(rs.getLong("userId"));
        data.setPatronsId(rs.getInt("patronsId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setConditionType(rs.getInt("conditionType"));
        data.setConditionId(rs.getInt("conditionId"));
        data.setCreateTime(rs.getLong("createTime"));
        return data;
    }
}
